{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e415037a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.session import Environment, GsSession\n",
    "import gs_quant.risk as risk\n",
    "from gs_quant.instrument import IRSwap, IRSwaption\n",
    "from gs_quant.markets.portfolio import Portfolio\n",
    "from gs_quant.config import DisplayOptions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e001d1f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1558474",
   "metadata": {},
   "outputs": [],
   "source": [
    "# portfolio construction\n",
    "swap_1 = IRSwap('Pay', '5y', 'EUR', fixed_rate=-0.005, name='5y')\n",
    "swap_2 = IRSwap('Pay', '10y', 'EUR', fixed_rate=-0.005, name='10y')\n",
    "swap_3 = IRSwap('Pay', '5y', 'USD', fixed_rate=-0.005, name='5y')\n",
    "swap_4 = IRSwap('Pay', '10y', 'USD', fixed_rate=-0.005, name='10y')\n",
    "swaption_1 = IRSwaption('Pay', '5y', 'USD', expiration_date='1y', name='5y')\n",
    "eur_port = Portfolio([swap_1, swap_2], name='EUR')\n",
    "usd_port = Portfolio([swap_3, swap_4], name='USD')\n",
    "nested_port = Portfolio([eur_port, usd_port, swaption_1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b770f93",
   "metadata": {},
   "outputs": [],
   "source": [
    "# risk calculations\n",
    "eur_port_price = eur_port.price()\n",
    "nested_port_price = nested_port.price()\n",
    "nested_port_vega = nested_port.calc(risk.IRVegaParallel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbf14a17",
   "metadata": {},
   "source": [
    "Pivot Parameters\n",
    "\n",
    "-  inherited from pandas.pivot_table()\n",
    "-  index and column values are the names of instruments and portfolios"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3dd1164b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# default to_frame()\n",
    "nested_port_price.to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6469cb11",
   "metadata": {},
   "outputs": [],
   "source": [
    "# to_frame() with no pivot\n",
    "nested_port_price.to_frame(values=None, columns=None, index=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "454e1851",
   "metadata": {},
   "outputs": [],
   "source": [
    "# modify to_frame() with custom pivot parameters - similar to pandas.pivot_table()\n",
    "nested_port_price.to_frame(values='value', columns='portfolio_name_0', index='instrument_name')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65d6a2f6",
   "metadata": {},
   "source": [
    "Aggregation\n",
    "-  inherited from pandas.pivot_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03514616",
   "metadata": {},
   "outputs": [],
   "source": [
    "swap_5 = IRSwap('Pay', '5y', 'EUR', fixed_rate=-0.005, name='5y')\n",
    "swap_6 = IRSwap('Pay', '10y', 'EUR', fixed_rate=-0.005, name='5y')\n",
    "port = Portfolio([swap_5, swap_6])\n",
    "res = port.price()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e441762",
   "metadata": {},
   "outputs": [],
   "source": [
    "# when instruments have the same name, the values are summed by default\n",
    "res.to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "267db8cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# change aggregation of values\n",
    "res.to_frame(aggfunc='mean')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ae17333",
   "metadata": {},
   "source": [
    "Show N/A values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d15564e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# to_frame() by default eliminates N/A values from the dataframe result\n",
    "nested_port_vega.to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a9797837",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pass in display_options to show N/A values\n",
    "nested_port_vega.to_frame(display_options=DisplayOptions(show_na=True))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
